#!/bin/bash
set -e
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./RECON-1.08.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi

wget -t 10 -c -P $installpath https://www.repeatmasker.org/RepeatModeler/RECON-1.08.tar.gz
cd $installpath
tar xf RECON-1.08.tar.gz
mv RECON-1.08 RECON && cd RECON
sed -i -e 's/gcc/clang/g' src/Makefile
make -j$(nproc) -C src && sudo make -C src install
cp 00README bin
sed -i "s#^\$path =.*#\$path = \"$installpath/RECON/bin\";#" scripts/recon.pl
rm -rf $installpath/RECON-1.08.tar.gz
cat>"$installpath/RECON/RECON_modulefiles"<<EOF
#%Module1.0
conflict RECON
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set RECON \$pwd
setenv RECON \$RECON
prepend-path PATH \$RECON/bin
EOF
